package com.doupi.partner.op.util;

import org.junit.Assert;

import com.doupi.partner.constant.PartnerTypeEnum;
import com.doupi.partner.model.BusinessRelation;
import com.doupi.partner.model.PartnerRelation;
import com.doupi.partner.model.Profit;
import com.doupi.partner.model.UserRelation;
import com.doupi.partner.service.dto.AdministratorDto;
import com.doupi.partner.util.RuntimeBusinessException;
import com.doupi.partner.util.constant.UtilConstants;

/**
 * 图表工具
 * 
 * @author Alvin Wang
 * @version 1.0.0 @ 2017-05-01
 */
public class ChartUtils {

	/**
	 * 不同用户登录查询不同
	 * 
	 * @param profit
	 *            收益查询参数
	 * @param businessRelation
	 *            商家关系查询参数
	 * @param userRelation
	 *            用户关系查询参数
	 * @param administratorDto
	 *            当前登录管理员
	 */
	public static void wrapParmas(Profit profit, BusinessRelation businessRelation, UserRelation userRelation, AdministratorDto administratorDto) {
		profit.setAdministratorId(administratorDto.getId());
		profit.setStatus(UtilConstants.YES); // 有效的收益

		// 如果是合伙人
		if (administratorDto.isPartner()) {
			PartnerRelation administratorRelation = administratorDto.getPartnerRelation();
			PartnerTypeEnum partnerType = PartnerTypeEnum.getPartnerTypeByCode(administratorRelation.getType());
			Assert.assertNotNull(partnerType);
			switch (partnerType) {
			case HEADQUARTERS:// 总站
				profit.setStatus(null); // 有效的收益
				profit.setAdministratorId(null);
				break;
			case SUBSTATION:// 分站
				businessRelation.setSubstationAdministratorId(administratorDto.getId());
				userRelation.setSubstationAdministratorId(administratorDto.getId());
				break;
			case CITY: // 城市代理商
				businessRelation.setCityAdministratorId(administratorRelation.getAdministratorId());
				userRelation.setCityAdministratorId(administratorRelation.getAdministratorId());
				break;
			case SUPERIOR: // 一级合伙人
				businessRelation.setPartnerAdministratorId(administratorDto.getId());
				userRelation.setPartnerAdministratorId(administratorDto.getId());
				businessRelation.setSuperiorPartnerAdministratorId(administratorRelation.getAdministratorId());
				userRelation.setSuperiorPartnerAdministratorId(administratorRelation.getAdministratorId());
				break;
			case SECOND:// 二级合伙人
				businessRelation.setPartnerAdministratorId(administratorDto.getId());
				userRelation.setPartnerAdministratorId(administratorDto.getId());
			default:
				throw RuntimeBusinessException.createException("未知的合伙人类型");
			}
		}

		if (administratorDto.isBusiness()) {
			BusinessRelation administratorBusinessRelation = administratorDto.getBusinessRelation();
			userRelation.setBusinessAdministratorId(administratorBusinessRelation.getAdministratorId());
		}
	}
}
